<?php

namespace app\admin\controller;

use app\BaseController;
use OSS\Core\OSSException;
use OSS\OSSClient;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
use think\facade\Filesystem;

class Uploads extends Common
{
    private $storage = 1; //1为本地存储
    private $storage_config = array();//云存储配置参数

    public function __construct()
    {
        parent::__construct();
//        $this->storage = $storage['id'];
//        $this->storage_config = json_decode($storage['value'], true);
    }

    /**
     * 普通图片上传
     * @date 2019-09-22
     */
    public function upimg()
    {
        try {
            $file = request()->file('file');    // 获取表单上传文件
            $savename = Filesystem::disk('public')->putFile('images', $file, function () {
                return date('Ymd') . "/" . mt_rand(10, 90) . "/" . md5((string)microtime(true));
            });
//            editImg(BASE_PATH.'/uploads/' . $savename,120,80);
            $pathurl = $this->ossUpload("uploads/" . $savename);//检测是否OSS存储
            if ($this->storage == 1) {
                return ['code' => 0, "url" => '/uploads/' . $savename, 'msg' => '上传成功'];
            } else {
                return ['code' => 0, "url" => $pathurl, 'msg' => '上传成功'];
            }
        } catch (think\exception\ValidateException $e) {
            return ['code' => 1, "url" => "/uploads/" . $savename, 'msg' => $e->getMessage()];
        }
    }


    /**
     * 普通图片上传
     * @date 2019-09-22
     */
    public function wangEditorUpImg()
    {
        $param = input('param.');
        halt($param);
        try {
            $file = request()->file('file');
            $savename = Filesystem::disk('public')->putFile('images', $file, function () {
                return date('Ymd') . "/" . mt_rand(10, 90) . "/" . md5((string)microtime(true));
            });
            $pathurl = $this->ossUpload("uploads/" . $savename);//检测是否OSS存储
            if ($this->storage == 1) {
                return ['code' => 0, "url" => '/uploads/' . $savename, 'msg' => '上传成功'];
            } else {
                return ['code' => 0, "url" => $pathurl, 'msg' => '上传成功'];
            }
        } catch (think\exception\ValidateException $e) {
            return ['code' => 1, "url" => "/uploads/" . $savename, 'msg' => $e->getMessage()];
        }
    }


    /**
     * layui上传图片
     * @date 2019-09-22
     */
    public function upLayerImg()
    {
        try {
            $file = request()->file('file');    // 获取表单上传文件
            $savename = Filesystem::disk('public')->putFile('images', $file, function () {
                return date('Ymd') . "/" . mt_rand(10, 90) . "/" . md5((string)microtime(true));
            });
            $pathurl = $this->ossUpload("uploads/" . $savename);//检测是否OSS存储
            if ($this->storage == 1) {
                return ['code' => 0, "url" => '/uploads/' . $savename, 'msg' => '上传成功'];
            } else {
                return ['code' => 0, "url" => $pathurl, 'msg' => '上传成功'];
            }
        } catch (think\exception\ValidateException $e) {
            return ['code' => 1, "url" => "/uploads/" . $savename, 'msg' => $e->getMessage()];
        }
    }

    /**
     * 编缉器上传
     * @date 2019-09-22
     */
    public function kindupload()
    {
        $file = request()->file('file');    // 获取表单上传文件
        try {
            $savename = Filesystem::disk('public')->putFile('images', $file, function () {
                return date('Ymd') . "/" . mt_rand(10, 90) . "/" . md5((string)microtime(true));
            });
            $pathurl = $this->ossUpload("uploads/" . $savename);//检测是否OSS存储
            if ($this->storage == 1) {
                return json(['error' => 0, "url" => '/uploads/' . $savename, 'msg' => '上传成功']);
            } else {
                return json(['error' => 0, "url" => $pathurl, 'msg' => '上传成功']);
            }
            return json(['error' => 0, 'url' => "/uploads/" . $savename, 'file_name' => $savename]);
        } catch (think\exception\ValidateException $e) {
            return ['error' => 1, "url" => "/uploads/" . $savename, 'msg' => $e->getMessage()];
        }
    }

    /**
     * 云存储检查
     * @author 四*川挚梦*科技有*限公司
     * @date 2019-09-22
     */
    public function ossUpload($uploadfile = '')
    {
        if (!$uploadfile || $this->storage == 1) return $uploadfile;
        switch ($this->storage) {
            //阿里云存储
            case 2:
                try {
                    $ossClient = new OssClient($this->storage_config['AccessKeyID'], $this->storage_config['AccessKeySecret'], $this->storage_config['Endpoint']);
                    $result = $ossClient->uploadFile($this->storage_config['Bucket'], $uploadfile, BASE_PATH . "/" . $uploadfile);
                    $pathurl = isset($result['info']['url']) ? str_replace(BASE_PATH, "", $result['info']['url']) : $result;
                    return $pathurl;
                } catch (OssException $e) {
                    return array('code' => 1, "url" => '', 'msg' => $e->getMessage());
                }
                break;
            //七牛云存储
            case 3:
                try {
                    $upManager = new UploadManager();
                    $auth = new Auth($this->storage_config['accessKey'], $this->storage_config['secretKey']);
                    $token = $auth->uploadToken($this->storage_config['Bucket']);// 生成上传 Token
                    list($ret,$err) = $upManager->putFile($token, $uploadfile, BASE_PATH . "/" . $uploadfile);
                    if ($err !== null) {
                        return false;
                    } else {
                        if ($this->storage_config['DOMAIN']) {
                            $url = $this->storage_config['DOMAIN'] . $uploadfile;
                        } else {
                            $url = "http://" . trim($this->storage_config['ENDPOINT']) . "/" . $uploadfile;
                        }
                        return $url;
                    }
                } catch (OssException $e) {
                    return array('code' => 1, "url" => '', 'msg' => $e->getMessage());
                }
                break;
        }


    }


}